package vns.server;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import vns.client.service.ObjectTypeService;
import vns.server.domain.ObjectPropertyType;
import vns.server.domain.ObjectType;
import vns.shared.ObjectPropertyTypeDTO;
import vns.shared.ObjectTypeDTO;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ObjectTypeServiceImpl extends RemoteServiceServlet implements
		ObjectTypeService {

	@Override
	public ObjectTypeDTO Create(ObjectTypeDTO otypeDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectType action = null;
		try {
			action = new ObjectType(otypeDTO);
			pm.makePersistent(action);
		} finally {
			pm.close();
		}
		return action.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ObjectType action = pm.getObjectById(ObjectType.class, id);
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(ObjectType.class, id));
			}
		} finally {
			pm.close();
		}
		return true;
	}

	@Override
	public ObjectTypeDTO Loadbyid(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectType action = null, detach = null;
		try {
			action = pm.getObjectById(ObjectType.class, id);
			detach = pm.detachCopy(action);
		} finally {
			pm.close();
		}
		return detach.toDTO();
	}

	@Override
	public ObjectTypeDTO Update(ObjectTypeDTO otypeDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectType action = null;
		try {
			action = pm.getObjectById(ObjectType.class, otypeDTO.getId());
			action.updatefromDTO(otypeDTO);
		} finally {
			pm.close();
		}

		return action.toDTO();
	}

	@Override
	public ArrayList<ObjectTypeDTO> Loadall(ObjectPropertyTypeDTO optypeDTO) {
		ArrayList<ObjectTypeDTO> listactionDTO = new ArrayList<ObjectTypeDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ObjectPropertyType actionset = pm.getObjectById(
					ObjectPropertyType.class, optypeDTO.getId());
		} finally {
			pm.close();
		}
		return listactionDTO;
	}

	@Override
	public void Updateall(List<ObjectTypeDTO> listNew,
			ObjectPropertyTypeDTO optypeDTO) {
		// TODO Auto-generated method stub

	}

	@Override
	public ArrayList<ObjectPropertyTypeDTO> Loadall(ObjectTypeDTO otypeDTO) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void Updateall(List<ObjectTypeDTO> listNew) {
		ArrayList<ObjectTypeDTO> listOld = this.Loadall();
		for (ObjectTypeDTO element : listNew) {
			if (!containInList(listOld,element)) {
				element = this.Create(element);
			} else {
				this.Update(element);
			}
		}
		for (ObjectTypeDTO element : listOld) {
			if (!containInList(listNew,element)) {
				this.Remove(element.getId());
			}
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<ObjectTypeDTO> Loadall() {
		ArrayList<ObjectTypeDTO> listactionDTO = new ArrayList<ObjectTypeDTO>();

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ObjectType.class);
		try {
			List<ObjectType> res = (List<ObjectType>) q.execute();
			for (ObjectType actiontype : res) {

				listactionDTO.add(actiontype.toDTO());
			}
		} finally {
			pm.close();
		}
		return listactionDTO;
	}

	private boolean containInList(List<ObjectTypeDTO> list,
			ObjectTypeDTO element) {
		if (element.getId() == null)
			return false;
		for (ObjectTypeDTO optDTO : list) {
			if (optDTO.getId().equals(element.getId()))
				return true;
		}
		return false;
	}
}
